package conexion;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class Comprar {
	
	public static void estadoPuja(String traderId, int puja){
		URL urlEstadoPuja;
		HttpsURLConnection connectionEstadoPuja = null;
		String lineEstadoPuja="0";
		String setCookie = Conexion.EASW_KEY + "; " + Conexion.easf_sess_com + "; FUTWebPhishing" + Conexion.FUTWebPhishing;
		
		try{
			urlEstadoPuja = new URL("https://utas.fut.ea.com/ut/game/fifa13/trade?tradeIds="+traderId);
			connectionEstadoPuja = (HttpsURLConnection)urlEstadoPuja.openConnection();
			connectionEstadoPuja.setRequestMethod("POST");
			connectionEstadoPuja.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
			connectionEstadoPuja.setRequestProperty("Cookie", setCookie);
			connectionEstadoPuja.setRequestProperty("Content-Type", "application/json");	
			connectionEstadoPuja.setRequestProperty("X-UT-SID", Conexion.XUTSID);	
			connectionEstadoPuja.setRequestProperty("X-HTTP-Method-Override", "GET");
			connectionEstadoPuja.setUseCaches (false);connectionEstadoPuja.setDoInput(true);connectionEstadoPuja.setDoOutput(true);		

			//Get Response	
			InputStream isEstadoPuja = connectionEstadoPuja.getInputStream();		
			BufferedReader rdEstadoPuja = new BufferedReader(new InputStreamReader(isEstadoPuja));		
			lineEstadoPuja = rdEstadoPuja.readLine();
			System.out.println(lineEstadoPuja);
			
			String tradeId = JsonParse.stadoTradeId(lineEstadoPuja);
			System.out.println("TradeId: "+tradeId);
			Thread.sleep(300);
			comprarContrato(traderId, puja);
			
		}catch(Exception e){}
	}
	
	public static void comprarContrato(String traderId, int puja){
		URL urlBuyUser;
		HttpsURLConnection connectionBuyUser = null;
		String lineBuyUser="0";
		String data="{\"bid\":"+puja+"}";
		String setCookie = Conexion.EASW_KEY + "; " + Conexion.easf_sess_com + "; FUTWebPhishing" + Conexion.FUTWebPhishing;

		try{
			urlBuyUser = new URL("https://utas.fut.ea.com/ut/game/fifa13/trade/"+traderId+"/bid");
			connectionBuyUser = (HttpsURLConnection)urlBuyUser.openConnection();
			connectionBuyUser.setRequestMethod("POST");
			connectionBuyUser.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
			connectionBuyUser.setRequestProperty("Cookie", setCookie);
			connectionBuyUser.setRequestProperty("Content-Type", "application/json");
			connectionBuyUser.setRequestProperty("Content-Length", "" +Integer.toString(data.getBytes().length)); 
			connectionBuyUser.setRequestProperty("X-UT-SID", Conexion.XUTSID);	
			connectionBuyUser.setRequestProperty("X-HTTP-Method-Override", "PUT");
			connectionBuyUser.setUseCaches (false);connectionBuyUser.setDoInput(true);connectionBuyUser.setDoOutput(true);		
			
			//Send request
			DataOutputStream wrBuyUser = new DataOutputStream (connectionBuyUser.getOutputStream ());
			wrBuyUser.writeBytes (data);
			wrBuyUser.flush ();
			wrBuyUser.close ();
					      
			//Get Response	
			InputStream isBuyUser = connectionBuyUser.getInputStream();		
			BufferedReader rdBuyUser = new BufferedReader(new InputStreamReader(isBuyUser));		
			lineBuyUser = rdBuyUser.readLine();		
			//System.out.println("\n"+lineBuyUser);
			if(lineBuyUser.indexOf("debug")!= -1){
				System.out.println("No Comprado");
			}else{
				System.out.println("Comprado");
			}
			
			Thread.sleep(300);
		}catch(Exception e){}
	}

}
